<%@page import="java.util.Map"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="page" uri="/WEB-INF/tlds/page.tld"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.maxjay.component.simplelist.web.page.Page"%>
<%@page import="com.maxjay.system.core.cache.SystemDataHolder"%>
<%@page import="com.maxjay.common.util.json.JsonParser"%>
<%@page import="com.maxjay.component.metadata.model.EntityField"%>
<%@page import="com.maxjay.component.simplelist.model.QueryListField"%>
<%@page import="com.maxjay.component.simplelist.model.QueryScheme"%>
<%
	Page slPage = (Page)request.getAttribute("page");
	pageContext.setAttribute("rowCount", slPage.getRowCount());
	QueryScheme queryScheme = (QueryScheme)request.getAttribute("queryScheme");
	pageContext.setAttribute("schemeId", queryScheme.getId());
	pageContext.setAttribute("entityId", queryScheme.getEntity().getId());
	List<String> fieldNameList = new ArrayList<String>();
	List<String> fieldNameCnList = new ArrayList<String>();
	for(QueryListField queryListField : queryScheme.getListFields()) {
		EntityField entityField = queryListField.getEntityField();
		fieldNameList.add(entityField.getName());
	}
	pageContext.setAttribute("fields", JsonParser.convertArrayToJson(queryScheme.getListFields()));
	pageContext.setAttribute("fieldNames", JsonParser.convertArrayToJson(fieldNameList));
	Map<String, Object> paramMap = (Map<String, Object>)request.getAttribute("paramMap");
	pageContext.setAttribute("paramMap", JsonParser.convertObjectToJson(paramMap));
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<title>${ queryScheme.name }</title>	
	<%@ include file="/common/common.jsp" %>
	<script type="text/javascript" src="<%=request.getContextPath() %>/component/simplelist/js/simpleList.js"></script>
	<script type="text/javascript">
		var tabId = '<%= request.getParameter("tabId") %>';
		var rowCountNum = <%= pageContext.getAttribute("rowCount") %>;
		var pgSize = <%= SystemDataHolder.PAGE_SIZE %>;
		var fields = eval('<%= pageContext.getAttribute("fields") %>');
		var fieldNames = eval('<%= pageContext.getAttribute("fieldNames") %>');
		var params = eval('(' + '<%= pageContext.getAttribute("paramMap") %>' + ')');
		var grid;
		var winEmp;
		var addUrl = '<%= request.getContextPath() %>/comp/entity?action=preCreate&entityId=<%= (String)pageContext.getAttribute("entityId") %>&tabId=' + tabId;
		var editUrl = '<%= request.getContextPath() %>/comp/entity?action=preUpdate&entityId=<%= (String)pageContext.getAttribute("entityId") %>&tabId=' + tabId + '&entityObjId=';
		var delUrl = '<%= request.getContextPath() %>/comp/entity?action=delete&entityId=<%= (String)pageContext.getAttribute("entityId") %>&entityObjId=';
		params.start = 0;
		params.limit = pgSize;
		params.rowCount = rowCountNum;
		Ext.onReady(function(){
			var myStore = new Ext.data.Store({
		        reader: new Ext.data.JsonReader({
					        totalProperty: "totalCount",
					        root: "records",
					        id:"OBJ_ID",
					        fields: fieldNames
					    }),
		        proxy:new Ext.data.HttpProxy({   
		                      url:"<%= request.getContextPath() %>/comp/simpleListData?schemeId=<%= (String)pageContext.getAttribute("schemeId") %>",//获取数据的后台地址   
		                      method:"POST"  
		                }),
		        baseParams:params
		    });
			var sm = new Ext.grid.CheckboxSelectionModel({   
				    	//dataIndex:"OBJ_ID",
				    	singleSelect:false
				    });   
			var index = new Ext.grid.RowNumberer();//行号 
			var cols = new Array();
			cols[0] = index;
			cols[1] = sm;
			for(var i=0; i<fields.length; i++){
				var col = new Object();
				col.header = fields[i].entityField.nameCn;
				col.dataIndex = fields[i].entityField.name;
				col.sortable = true;
				if(!fields[i].display){
					col.hidden = true;
				}
				cols[i+2] = col;
			}
			//cols.defaultSortable = true; 统一设置各列都可作为排序列
			grid = new Ext.grid.GridPanel({
				id:'slGrid',
				renderTo:'grid',
				loadMask:true,
			    columns: cols,
			    sm: sm,
			    store: myStore,
			    width:'100%',
			    height:'500',
			    frame:true,
			    title:'',
			    iconCls:'icon-grid',
			    trackMouseOver:true, //鼠标特效   
		        autoScroll:true,   
		        stripeRows:true,
		        viewConfig: {
		        	forceFit:true //使每列都自动扩充，让列表更美观
		        },
		        tbar:[
		              {                    
		                  text: '新增',
		                  cls: 'add',
		                  handler: addEntity
		              },
		              '-', 
		              {
		            	  text: '修改',
		                  cls: 'edit',
		                  handler: editEntity
		              },
		              '-', 
		              {
		            	  text: '删除',
		                  cls: 'delete',
		                  handler: deleteEntity
		              }
		             ],
			    bbar: new Ext.PagingToolbar({
		        	pageSize:pgSize,
		        	store: myStore,
		        	displayInfo:true,
		        	displayMsg:'显示第{0}条到{1}条记录，共{2}条',
		        	emptyMsg:'没有记录',
		        })
			});
			myStore.load({
				//params:{start:0,limit:pgSize,rowCount:rowCountNum},
				callback:function(records, options, success){
					//rowCountNum = myStore.getTotalCount();
				}
			});
			//grid.render("grid");
		});
	</script>
</head>
<body>
	<div id="grid"></div>
</body>
</html>